﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GLImp;
using System.Drawing;
using CastleOfTheDisillusioned.Particles;
using CastleOfTheDisillusioned.Location;

namespace CastleOfTheDisillusioned.Enemies {
	class Zombie : Enemy {
		double Speed = 0.015f;
		int TimeAlive;

		Sprite WalkLeft;

		public Zombie(double X, double Y) {
			this.X = X;
			this.Y = Y;
			this.Width = 1;
			this.Height = 2;

			Facing = ((Location_State.CurrentPlayer.X - this.X) < 0) ? Direction.Left : Direction.Right;
			TimeAlive = Program.Random.Next(60 * 10, 60 * 20);

			Speed = ((Program.Random.NextDouble() * 0.01) + 0.01);

			Health = 10;

			Texture z = Textures.ZombieGhoul;
			WalkLeft = new Sprite();
			WalkLeft.Add(z.Subimage(12, 56, 24, 48)); //0
			WalkLeft.Add(z.Subimage(41, 56, 24, 48));
			WalkLeft.Add(z.Subimage(69, 56, 24, 48));
			WalkLeft.Add(z.Subimage(96, 56, 24, 48));

			WalkLeft.Add(z.Subimage(12, 110, 24, 48));
			WalkLeft.Add(z.Subimage(43, 110, 24, 48));
			WalkLeft.Add(z.Subimage(71, 110, 24, 48));
			WalkLeft.Add(z.Subimage(98, 110, 24, 48)); //7

			WalkLeft.Add(z.Subimage(98, 110, 24, 48)); //8
			WalkLeft.Add(z.Subimage(71, 110, 24, 48));
			WalkLeft.Add(z.Subimage(43, 110, 24, 48));
			WalkLeft.Add(z.Subimage(12, 110, 24, 48));

			WalkLeft.Add(z.Subimage(96, 56, 24, 48));
			WalkLeft.Add(z.Subimage(69, 56, 24, 48));
			WalkLeft.Add(z.Subimage(41, 56, 24, 48));
			WalkLeft.Add(z.Subimage(12, 56, 24, 48)); //15	

			WalkLeft.PlaybackSpeed = 0.1;
		}

		public override void Draw(){
			//GraphicsManager.DrawRectangle(X, Y, Width, Height, Color.FromArgb(0x60, 0xA3, 0x4D));
			if (Facing == Direction.Left) {
				WalkLeft.Draw(X, Y, Width, Height);
			} else {
				WalkLeft.Draw(X + 1, Y, -Width, Height);
			}
		}

		public override void Update() {
			base.Update();
			if ((WalkLeft.CurrentFrame >= 1 && WalkLeft.CurrentFrame < 7) ||
				(WalkLeft.CurrentFrame >= 9 && WalkLeft.CurrentFrame < 15)) {
				if (Facing == Direction.Left) {
					Move(-Speed, 0);
				} else {
					Move(Speed, 0);
				}
			}

			if (--TimeAlive <= 0) {
				Destroy();
			}
		}

		public override void Destroy() {
			base.Destroy();

			List<Subimage> Gibs = new List<Subimage>();
			//Left Half
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 54, 317, 11, 8));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 52, 327, 11, 13));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 70, 328, 7, 8));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 84, 324, 4, 6));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 53, 345, 9, 11));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 66, 344, 8, 12));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 85, 334, 6, 17));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 51, 361, 9, 11));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 65, 361, 11, 11));
			Gibs.Add(new Subimage(Textures.ZombieGhoul, 81, 356, 15, 16));

			foreach (Subimage img in Gibs) {
				ImageParticle p = new ImageParticle(img, X + (Program.Random.NextDouble() * Width), Y + (Program.Random.NextDouble() * Height));
				p.HSpeed = (Program.Random.NextDouble() - 0.5) * 0.1;
				p.VSpeed = Program.Random.NextDouble() * -0.2;
				p.Lifespan = 60;
				p.Width = p.Width / 20;
				p.Height = p.Height / 20;
				p.Gravity = 0.01;

				ParticleManager.Track(p);
			}

		}
	}
}
